package m.jclecai.com.activity;

import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnTextChanged;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.ProofBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

public class ProductSuggestActivity extends BaseActivity {


    @BindView(R.id.et_suggest_content)
    EditText mEtSuggestContent;

    @BindView(R.id.tv_can_intput_size)
    TextView mTvCanIntputSize;

    int mSuggestMaxLength=255;

    @Override
    protected int getContentView() {
        return R.layout.activity_product_suggest;
    }

    @Override
    protected void initView() {
        setTitle("意见反馈");
        rightOptionText("提交留言");
        leftOptionText("");
        mTvCanIntputSize.setText(Html.fromHtml("<html>还可以输入<font color=\"#3097fd\">"+mSuggestMaxLength+"</font>字</html>"));
    }


    @Override
    protected void initData() {
        tvRightOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //这里我们要做的一个操作就是提交建议；
                commitSuggestion();
            }
        });
    }

    /**
     * 这里我们要做的一个操作就是体积哦啊
     */
    private void commitSuggestion() {

        String s = mEtSuggestContent.getText().toString();
        if(TextUtils.isEmpty(s)){
            DisPlay("建议反馈不能为空！！！");
            return;
        }

        showDialog();
        HashMap<String, String> params = new HashMap<>();
        params.put("user",PreferenceUtil.getUserName(this));
        params.put("password",PreferenceUtil.getPassword(this));
        params.put("content",s);
        HttpManager.postAsyn(Constant_Url.NOTICEADD, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                DisPlay("请检查网络");
                e.printStackTrace();
            }

            @Override
            public void onResponse(String response) {
                //这里我们要做第一个操作就是提交成；
                dissDialog();
                try {
                    ProofBean proofBean = FastOk.get(response, ProofBean.class);
                    if ("0".equals(proofBean.getStatus())) {
                        //这里表示提交成功；
                        DisPlay("提交成功！！！");
                        ProductSuggestActivity.this.finish();
                    }else {
                        DisPlay(proofBean.getMessage());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    DisPlay("网络异常！");
                }
            }
        },params);

    }

    @OnTextChanged(R.id.et_suggest_content)
    public void onEditTextChange(CharSequence s, int start, int count, int after){

        String string = "";
        string=s.toString().trim();
        int length=string.length();
        mEtSuggestContent.setEnabled(true);
        //etText最多只能设置255个字符
        mTvCanIntputSize.setText(Html.fromHtml("<html>还可以输入<font color=\"#3097fd\">"+(mSuggestMaxLength-length)+"</font>字</html>"));
    }

}
